
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:验证支付密码状态
 *
 * @author wcj
 * @since 2015年12月9日 下午2:59:53
 */
public class CheckPayPasswdStateTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public CheckPayPswStateResponse request(String userName) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(userName);
        CheckPayPswStateResponse respObj = new CheckPayPswStateResponse();
        ResponsePackage resp = new CheckAnswerResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_CHECK_PAY_PSW_STATE);
        params.put("username", userName);
        requestParams.add(params);
        mCmdList.add(ProtocolCmd.CMD_CHECK_PAY_PSW_STATE);

        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class CheckAnswerResponsePackage extends HttpBaseResponsePackage<CheckPayPswStateResponse> {

        @Override
        public void handleResponse(CheckPayPswStateResponse resetPasswordResponse, String jsonStr) {
            try {
                LogUtil.d("response", "CheckPayPasswdStateTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    resetPasswordResponse.setCode(code);
                    resetPasswordResponse.setMessage(message);
                    resetPasswordResponse.setPrompt(prompt);

                    if (resetPasswordResponse.isUseful()
                            && cmd.equals(ProtocolCmd.CMD_CHECK_PAY_PSW_STATE)) {
                        JSONObject data = jsonResp.optJSONObject("data");

                        resetPasswordResponse.setSeted(data.optInt("is_setpaypwd") == 1);
                        resetPasswordResponse.setLocked(data.optInt("is_freeze") == 1);
                    }
                    resetPasswordResponse.setOk(true);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 响应消息
     */
    public static class CheckPayPswStateResponse extends ResponseMessage {

        private boolean isPayPasswdSeted;

        private boolean isLocked;

        public boolean isPayPasswdSeted() {
            return isPayPasswdSeted;
        }

        public void setSeted(boolean isSeted) {
            this.isPayPasswdSeted = isSeted;
        }

        public boolean isLocked() {
            return isLocked;
        }

        public void setLocked(boolean isLocked) {
            this.isLocked = isLocked;
        }
    }
}
